package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.LogMana;
import com.sdry.service.llm.LogManaService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 系统日志controller
* @author lian 
* @version 1.0
* @time 2018年10月16日 上午11:53:27
*/
@Controller
@RequestMapping("/logMana")
public class SystemLogController {

	@Resource
	private LogManaService logManaService;
	
	/**
	 * 分页查询日志信息
	 * @param page 分页条件
	 * @param bom 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageLog")
	public void listPageLog(Page page, LogMana logMana, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (logMana.getUserName() != null ) {
			map.put("userName", logMana.getUserName());
		} else {
			map.put("userName", "");
			logMana.setUserName("");
		}
		int count = logManaService.countLog(logMana);
		List<LogMana> BomList = logManaService.listPageLog(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(BomList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存日志信息
	 * @param logMana 日志信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveLog")
	public void saveLog(LogMana logMana, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		isSuccess = logManaService.savaLog(logMana);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除日志
	 * @param str lid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteLog")
	public void deleteLog(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = logManaService.deleteLog(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除一个月以前的日志
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteByMonth")
	public void deleteByMonth(HttpServletResponse response) throws Exception{
		int isSuccess = logManaService.deleteByMonth();
		ResponseUtil.write(response, isSuccess);
	}
}
